﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmDinhKhoanPhieuXuatKho : Form
    {
        #region Khai bao cuc bo

        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private PhieuXuatKhoManagement phieuxuatkhoMgt = new PhieuXuatKhoManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();

        public long tien = 0;
        public long tongtien = 0;

        private PhieuXuatKho phieuxuatkho;
        public PhieuXuatKho PhieuXuatKho
        {
            set { this.phieuxuatkho = value; }
            get { return this.phieuxuatkho; }
        }

        #endregion
       
        #region Xu ly Load

        public FrmDinhKhoanPhieuXuatKho(long tient)
        {
            InitializeComponent();
            this.tien = tient;
        }

        private void FrmDinhKhoanPhieuXuatKho_Load(object sender, EventArgs e)
        {
            ResultObj result = new ResultObj();
            result = phieuxuatkhoMgt.GetAllPhieuXuatKho();
            List<PhieuXuatKho> lstPhieuXuatKho = (List<PhieuXuatKho>)result.Result;
            txtSoChungTu.Text = phieuxuatkho.MaSo;
            List<ChiNhanhCongTy> lstChiNhanhCongTy = (from c in GetAllChiNhanhCongTy()
                                                      where c.Id == phieuxuatkho.IdChiNhanhCongTy
                                                      select c).ToList();
            if (lstChiNhanhCongTy.Count > 0)
            {
                txtChiNhanh.Text = lstChiNhanhCongTy[0].TenChiNhanh;
                txtMSKH.Text = lstChiNhanhCongTy[0].TenChiNhanh;
            }

            dgvDinhKhoanPhieuXuatKho.Columns["colSoTien"].ReadOnly = false;

            cboSoTaiKhoan.DataSource = GetAllTaiKhoanKeToan();
            cboSoTaiKhoan.DisplayMember = "MaSoTK";
            cboSoTaiKhoan.ValueMember = "TenTK";



            //AddToGrid();
        }

        //public void AddToGrid()
        //{
        //    DataGridViewRow dataRow = new DataGridViewRow();
        //    DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
        //    List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();
        //    TaiKhoanKeToan tkkt = new TaiKhoanKeToan();
        //    // Search value MaSoTK = "1521"

        //    lstTaiKhoanKeToan = (from tk in GetAllTaiKhoanKeToan()
        //                         where tk.MaSoTK == "1521"
        //                         select tk).ToList();
        //    for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
        //    {
        //        if (lstTaiKhoanKeToan[i].MaSoTK == "1521")
        //            tkkt = lstTaiKhoanKeToan[i];
        //    }

        //    cboCell.DataSource = lstTaiKhoanKeToan;
        //    cboCell.DisplayMember = "MaSoTK";
        //    cboCell.ValueMember = "TenTK";
        //    cboCell.Value = tkkt.TenTK;
        //    dataRow.Cells.Add(cboCell);

        //    cboCell = new DataGridViewComboBoxCell();
        //    cboCell.Items.Add("Nợ");
        //    cboCell.Items.Add("Có");
        //    cboCell.Value = "Có";
        //    dataRow.Cells.Add(cboCell);

        //    DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
        //    cell.Value = tkkt.TenTK; //name of 1521 
        //    dataRow.Cells.Add(cell);

        //    cell = new DataGridViewTextBoxCell();
        //    for (int j = 0; j < this.phieuxuatkho.listCTPhieuXuatKho.Count; j++)
        //    {
        //        cell.Value = this.phieuxuatkho.listCTPhieuXuatKho[j].ThanhTien;
        //    }
        //    dataRow.Cells.Add(cell);

        //    dgvDinhKhoanPhieuXuatKho.Rows.Add(dataRow);
        //}

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        #endregion
       
        #region Xu ly cac Button

        private void btnLuu_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < dgvDinhKhoanPhieuXuatKho.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[0].FormattedValue.ToString() == "" || dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[1].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            if (tien == tongtien)
            {
                EditOrRemoveDinhKhoan(phieuxuatkho.MaSo);

                ResultObj result = new ResultObj();
                
                for (int i = 1; i < dgvDinhKhoanPhieuXuatKho.Rows.Count - 1; i++)
                {
                    //if (dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[3].FormattedValue.ToString() != "0")
                    {
                        ButToan buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgay.Value;
                        buttoan.ChiNhanh = txtChiNhanh.Text;
                        buttoan.MaSoKhachHang = txtMSKH.Text;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = txtSoChungTu.Text;
                        buttoan.TKGoc = dgvDinhKhoanPhieuXuatKho.Rows[0].Cells[0].FormattedValue.ToString();
                        buttoan.TKDoiUng = dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[0].FormattedValue.ToString();
                        buttoan.GhiNo = 0;
                        buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[3].Value.ToString());
                        result = buttoanMgt.SaveNewButToan(buttoan);

                        buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgay.Value;
                        buttoan.ChiNhanh = txtChiNhanh.Text;
                        buttoan.MaSoKhachHang = txtMSKH.Text;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = txtSoChungTu.Text;
                        buttoan.TKGoc = dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[0].FormattedValue.ToString();
                        buttoan.TKDoiUng = dgvDinhKhoanPhieuXuatKho.Rows[0].Cells[0].FormattedValue.ToString();
                        buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[3].FormattedValue.ToString());
                        buttoan.GhiCo = 0;
                        result = buttoanMgt.SaveNewButToan(buttoan);

                    }
                }
                if (result.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");
                    this.Close();
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                else
                    MessageBox.Show("Thất bại");
            }
            else
            {
                MessageBox.Show("Số tiền định khoản không đúng", "Thông báo");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Xu ly cac su kien

        private void dgvDinhKhoanPhieuXuatKho_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                string tenTK = dgvDinhKhoanPhieuXuatKho.Rows[e.RowIndex].Cells[0].Value.ToString();
                dgvDinhKhoanPhieuXuatKho.Rows[e.RowIndex].Cells[2].Value = tenTK;
            }
            if (e.ColumnIndex == 3 && e.RowIndex > -1 && dgvDinhKhoanPhieuXuatKho.Rows[e.RowIndex].Cells[0].FormattedValue.ToString() != "")
            {
                if (dgvDinhKhoanPhieuXuatKho.Rows[e.RowIndex].Cells[3].Value == null)
                    dgvDinhKhoanPhieuXuatKho.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            long tong = 0;
            for (int i = 1; i < dgvDinhKhoanPhieuXuatKho.Rows.Count - 1; i++)
            {

                if (dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[3].Value != null)
                {
                    tong += Convert.ToInt64(dgvDinhKhoanPhieuXuatKho.Rows[i].Cells[3].Value.ToString());
                }
            }
            tongtien = tong;

            dgvDinhKhoanPhieuXuatKho.Rows[0].Cells[3].Value = tong;
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoanPhieuXuatKho.CurrentRow != null)
            {
                if (dgvDinhKhoanPhieuXuatKho.CurrentRow.Index != (dgvDinhKhoanPhieuXuatKho.Rows.Count - 1))
                {
                    dgvDinhKhoanPhieuXuatKho.Rows.RemoveAt(dgvDinhKhoanPhieuXuatKho.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoanPhieuXuatKho.Rows[dgvDinhKhoanPhieuXuatKho.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoanPhieuXuatKho.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    this.tongtien = result;
                    dgvDinhKhoanPhieuXuatKho.Rows[0].Cells[3].Value = result;
                }
                    
            }
        }

        private void dgvDinhKhoanPhieuXuatKho_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoanPhieuXuatKho.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoanPhieuXuatKho.ClearSelection();
                    dgvDinhKhoanPhieuXuatKho.CurrentCell = dgvDinhKhoanPhieuXuatKho.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoanPhieuXuatKho.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        #endregion

        #region Lay cac List

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj result = new ResultObj();
            result = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstchinhanhcongty = (List<ChiNhanhCongTy>)result.Result;
            return lstchinhanhcongty;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj result = new ResultObj();
            result = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)result.Result;
            return lstTaiKhoanKeToan;
        }

        #endregion
    }
}
